Public Class CustomerContactDialog
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents btnSubmit As System.Web.UI.WebControls.Button
    Protected WithEvents lblErr As System.Web.UI.WebControls.Label
    Protected WithEvents btnReset As System.Web.UI.HtmlControls.HtmlInputButton

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region
    Protected WithEvents CustomerContactCtr As CustomerContactControl

    Private CUSTOMER_CONTACT_PAGEBO_SESSION_NAME As String = "MIMS_WRS.Web.CustomerContact.PageBO"

#Region "   Properties"
    Protected ReadOnly Property CustomerID() As Integer
        Get
            Dim id As Integer

            Try
                id = CInt(Request.QueryString("cid"))
            Catch ex As Exception
                id = 0
            End Try

            If Not (id > 0) Then
                Throw New ApplicationException("Invalid or missing parameter")
            End If

            Return id
        End Get
    End Property

    Protected ReadOnly Property ContactID() As Integer
        Get
            Dim id As Integer

            If IsNothing(Request.QueryString("id")) Then
                Return 0
            End If

            Try
                id = CInt(Request.QueryString("id"))

                If Not (id >= 0) Then
                    Throw New Exception
                End If

            Catch ex As Exception
                Throw New ApplicationException("Invalid parameter", ex)
            End Try

            Return id
        End Get
    End Property

    Protected Property PageBO() As CCustomerContact
        Get
            Return CType(Session(Me.CUSTOMER_CONTACT_PAGEBO_SESSION_NAME), CCustomerContact)
        End Get
        Set(ByVal Value As CCustomerContact)
            Session(Me.CUSTOMER_CONTACT_PAGEBO_SESSION_NAME) = Value
        End Set
    End Property

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        If Not Page.IsPostBack Then

            Dim obj As New CCustomerContact

            If Me.ContactID > 0 Then
                Try
                    obj.Load(Me.ContactID)
                Catch ex As Exception
                    Throw New ApplicationException("Failed to load customer contact data", ex)
                End Try
            Else
                obj.Init()
                obj.CustomerID = Me.CustomerID
                Me.btnReset.Visible = False
            End If

            Me.PageBO = obj
            Me.CustomerContactCtr.PageBOSessionName = Me.CUSTOMER_CONTACT_PAGEBO_SESSION_NAME

        End If
    End Sub

    Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
        Try
            PageBO.Save()
            Me.SetReturnValue()
        Catch ex As Exception
            Me.lblErr.Text = ex.Message
        End Try
    End Sub

    Private Sub SetReturnValue()
        Dim s As String = ""
        s += "var obj= new Object();"
        s += String.Format("obj.ID = '{0}';", PageBO.ID)
        s += String.Format("obj.Name = '{0}';", PageBO.ContactName)
        s += "window.returnValue = obj;"
        s += "window.close();"
        Response.Write(String.Format("<script>{0}</script", s))
    End Sub
End Class

